import { listUpdate, tableUpdate } from '../../../../../common/utils/table.js';
import { initExecute } from '../../../../../common/utils/index.js';
import { _SuperTable } from '../../../../../../config.js';
import open from '../../../../../common/utils/open.js';
import TableSelection from './TableSelection.js';
const SuperTable = (hook, { JiessTable, Loading, Card, Spin }) => {
	return {
		name: 'SuperTable',
		isJiess: true,
		setup({
			beforeCreate,
			created,
			beforeMounte,
			mounted,
			beforeUpdate,
			updated,
			// ===========
			filter,
			container,
			// antd的table参数
			tableProps,
			// 列表容器的参数
			tableBoxParam = {},
			// 分页容器的参数
			pagerBoxParam = {},
			// 列表容器参数，常作用于card
			containerParam = {},
			// ===========
			...baseParam
		}) {
			const { $ref } = hook.$m;
			const boxParam = {
				// 列表左上标题
				title: '列表',
				// 默认带有列表card
				isCard: true,
				// 注入初始的全局配置
				..._SuperTable.boxParam
			};
			// 默认启用复选框
			const {
				// 列表表头配置
				items,
				// 初始化整体布局
				initLayout = _SuperTable.initLayout,
				// 是否开启复选框
				isCheckbox = true,
				// 复选参数配置
				selectionParam = [],
				// 列表接口
				httpApi,
				// 接口身份标识
				httpKey,
				// 列表唯一标记
				httpTag = '',
				// 是否立即加载列表
				immedicat = true,
				// 默认带分页的数据更新
				updateType = 'listUpdate',
				// 列表更新参数
				updateParam,
				// JiessTable的参数
				...other
			} = baseParam;
			// ==============================================================
			const extraParam = { ctx: this };
			// 列表配置对象
			tableProps = { scroll: { x: 'max-content' }, ...tableProps };
			if (isCheckbox) {
				// 构建列表复选框对象
				const tableSelection = new TableSelection(hook, ...selectionParam);
				// 构建列表复选框响应式数据
				const { rowSelection } = extraParam.tableSelection = tableSelection;
				// 注入响应式复选配置
				tableProps.rowSelection = { $proxy: rowSelection };
			}
			// ==============================================================
			const getSpining = () => {
				if (httpKey) {
					const spinning = $ref(false);
					// httpKey与httpTag的组合，构建一个唯一的列表标识
					new Loading(httpKey + httpTag, [httpKey], (boo) => (spinning.value = boo));
					return { is: Spin, spinning: { $proxy: spinning } };
				}
			}
			// --------------------------------------------
			let execFunc = null;
			// ==============================================================
			this.add({
				items,
				tableProps,
				is: JiessTable,
				create: async (midder) => {
					execFunc = initExecute.call(this, midder);
					execFunc(beforeCreate);
					Object.assign(boxParam, execFunc(container));
					midder.tableTAG = { title: boxParam.title, httpTag };
					midder.open = open.call(this, midder);
					execFunc(created);
				},
				render(midder, { table, pager }) {
					// 构建筛选区虚拟节点
					const style = { marginTop: '8px' };
					// 列表容器头部节点
					const { isCard, title, extra, ...otherBoxParam } = boxParam;
					if (isCard) {
						containerParam.is = Card;
						// 构建左侧标题
						if (title) containerParam.title = title;
						// 构建右侧工具
						if (extra) containerParam.extra = extra;
					}
					// 注入默认的分页容器样式，使分页和列表保持一定间隙
					if (!pagerBoxParam.style) pagerBoxParam.style = style;
					if (!tableBoxParam.style) tableBoxParam.style = style;
					const tableNode = [
						getSpining(),
						{
							...otherBoxParam,
							...containerParam,
							children: [
								{ $slot: 'tableRenderHeader' },
								table,
								{ $slot: 'tableRenderFooter' },
								// 可通过$if控制是否显示分页
								[pagerBoxParam, pager]
							]
						}
					]
					if (typeof initLayout === 'function') {
						this.add(initLayout.call(this, midder, tableNode));
					} else {
						this.add(execFunc(filter), tableBoxParam, tableNode);
					}
				},
				mounte: async (midder) => {
					execFunc(beforeMounte);
					// 初始化是否调用update更新列表
					if (immedicat) await midder.table.reload();
					execFunc(mounted);
				},
				update: async (midder) => {
					execFunc(beforeUpdate);
					const update = updateType === 'tableUpdate' ? tableUpdate : listUpdate;
					await update.call(this, httpApi, midder, updateParam);
					execFunc(updated);
				},
				...other
			})
		}
	}
};
export default SuperTable;